﻿
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Xml.Serialization;

namespace MedVis_MidiController
{
    [XmlRoot("MidiDeviceSettings")]
    [XmlInclude(typeof(MidiControllerSettings))] // include type class Person
    public class MidiDeviceSettings
    {
        [XmlArray("MidiControllerSettingsList")]
        [XmlArrayItem("MidiControllerSettings")]
        public List<MidiControllerSettings> midiControllerSettings = new List<MidiControllerSettings>();


        [XmlIgnore]
        public Bitmap backgroundImage { get; set; }

        //[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        [XmlElement("BackgroundImage")]
        public byte[] backgroundImageBytes
        {
            get
            { // serialize
                if (backgroundImage == null) return null;
                try
                { 
                    using (MemoryStream ms = new MemoryStream())
                    {
                        (new Bitmap(backgroundImage)).Save(ms, ImageFormat.Bmp);
                        return ms.ToArray();
                    }
                 }
                catch (Exception exc)
                {}
                return null;
            }
            set
            { // deserialize
                if (value == null)
                    backgroundImage = null;
                else
                {
                    try
                    { 
                        using (MemoryStream ms = new MemoryStream(value))
                        {
                            backgroundImage = new Bitmap(ms);
                        }
                     }
                    catch(Exception exc)
                    {
                        backgroundImage = null;
                    }
                }
            }
        }

        public MidiControllerSettings addController(byte controlNumber)
        {
            if (controlNumber < 0)
                return null;

            var oldEntry = contains(controlNumber);
            if (oldEntry != null)
            {
                return oldEntry;
            }
            MidiControllerSettings mCS = new MidiControllerSettings(controlNumber);
            midiControllerSettings.Add(mCS);

            return mCS;
        }
        public void clearController()
        {
            midiControllerSettings.Clear();
        }

        public MidiControllerSettings contains(int controlNumber)
        {
            return midiControllerSettings.Find(
                delegate(MidiControllerSettings mCS)
                {
                    return mCS.controlNumber == controlNumber;
                }
            ) ;
        }


        public void SaveToFile(string FileName)
        {
            using (var writer = new System.IO.StreamWriter(FileName))
            {
                var serializer = new XmlSerializer(typeof(MidiDeviceSettings));
                serializer.Serialize(writer, this);
                writer.Flush();
            }
        }
        public static MidiDeviceSettings LoadFromFile(string FileName)
        {
            using (var stream = System.IO.File.OpenRead(FileName))
            {
                var serializer = new XmlSerializer(typeof(MidiDeviceSettings));
                return serializer.Deserialize(stream) as MidiDeviceSettings;
            }
        }
    }
}
